﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ServiceModel;
using BankApplication.BusinessLogic;

namespace BankApplication.UI
{
	class ServerInteractionExceptionCatcher
	{
		public void Execute(Action func)
		{
			try
			{
				func();
			}
			catch (CommunicationException)
			{
				MessageBox.Show("Connection with server lost! Need to reestablish the session", 
					"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

				LoginForm loginForm = new LoginForm();
				var result = loginForm.ShowDialog();

				if (result == DialogResult.Cancel)
				{
					Application.Exit();
				}
			}
			catch (ServerException ex)
			{
				MessageBox.Show("Server error! " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
			catch (NotImplementedException)
			{
				MessageBox.Show("Not implemented!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
			catch (Exception ex)
			{
				MessageBox.Show("Unknown error encountered! Terminating..." + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
				Application.Exit();
			}
		}
	}
}
